package cn.zust.edu.cn;

import javax.swing.*;
import java.awt.*;

/**
 * 
 * @author czg
 * @since 2023/3/7 10:50
 */
public class Rectangle implements IShape{


    private Point ptTopLeft;

    private Point ptTopRight;

    private int width;

    private int height;

    public Rectangle() {
    }

    public Rectangle(Point ptTopLeft, int width, int height) {
        this.ptTopLeft = ptTopLeft;
        this.width = width;
        this.height = height;
        this.ptTopRight = new Point(ptTopLeft.x + width,ptTopLeft.y);
    }

    public double perimeter(){
        return (width + height)*2;
    }

    public double area(){
        return width * height;
    }

    public void draw(){
        double perimeter = perimeter();
        double area = area();
        System.out.println("绘制长方形 ——— 周长: " + perimeter + ",面积:" + area);

        JFrame frame = new JFrame("绘制长方形");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);

        JPanel panel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.RED);
                g.drawRect(ptTopLeft.x, ptTopLeft.y, width, height);
            }
        };

        frame.add(panel);
        frame.setVisible(true);
    }
}
